package o;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.bumptech.glide.Glide;
import com.bumptech.glide.gifdecoder.GifDecoder;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

class xv {
    private boolean ʻ;
    private ro<Bitmap> ʼ;
    private boolean ʽ;
    private final List<b> ˊ;
    private c ˊॱ;
    private final Handler ˋ;
    private boolean ˋॱ;
    final rp ˎ;
    private final uc ˏ;
    private c ˏॱ;
    private Bitmap ͺ;
    private final GifDecoder ॱ;
    private si<Bitmap> ॱˊ;
    private int ॱˋ;
    @Nullable
    private d ॱˎ;
    private boolean ॱॱ;
    private int ॱᐝ;
    private c ᐝ;
    private int ᐝॱ;

    public interface b {
        void ʻ();
    }

    class a implements Callback {
        final /* synthetic */ xv ˏ;

        a(xv xvVar) {
            this.ˏ = xvVar;
        }

        public boolean handleMessage(Message message) {
            if (message.what == 1) {
                this.ˏ.ˏ((c) message.obj);
                return true;
            }
            if (message.what == 2) {
                this.ˏ.ˎ.ˎ((c) message.obj);
            }
            return false;
        }
    }

    @VisibleForTesting
    static class c extends zw<Bitmap> {
        private final Handler ˊ;
        private Bitmap ˋ;
        final int ˏ;
        private final long ॱ;

        public /* synthetic */ void onResourceReady(@NonNull Object obj, @Nullable aaf o_aaf) {
            ॱ((Bitmap) obj, o_aaf);
        }

        c(Handler handler, int i, long j) {
            this.ˊ = handler;
            this.ˏ = i;
            this.ॱ = j;
        }

        Bitmap ˎ() {
            return this.ˋ;
        }

        public void ॱ(@NonNull Bitmap bitmap, @Nullable aaf<? super Bitmap> o_aaf__super_android_graphics_Bitmap) {
            this.ˋ = bitmap;
            this.ˊ.sendMessageAtTime(this.ˊ.obtainMessage(1, this), this.ॱ);
        }

        public void onLoadCleared(@Nullable Drawable drawable) {
            this.ˋ = null;
        }
    }

    @VisibleForTesting
    interface d {
        void ˊ();
    }

    xv(Glide glide, GifDecoder gifDecoder, int i, int i2, si<Bitmap> siVar, Bitmap bitmap) {
        this(glide.ˋ(), Glide.ˏ(glide.ˏ()), gifDecoder, null, ˎ(Glide.ˏ(glide.ˏ()), i, i2), siVar, bitmap);
    }

    xv(uc ucVar, rp rpVar, GifDecoder gifDecoder, Handler handler, ro<Bitmap> roVar, si<Bitmap> siVar, Bitmap bitmap) {
        this.ˊ = new ArrayList();
        this.ˎ = rpVar;
        if (handler == null) {
            handler = new Handler(Looper.getMainLooper(), new a(this));
        }
        this.ˏ = ucVar;
        this.ˋ = handler;
        this.ʼ = roVar;
        this.ॱ = gifDecoder;
        ˎ(siVar, bitmap);
    }

    void ˎ(si<Bitmap> siVar, Bitmap bitmap) {
        this.ॱˊ = (si) aas.ˎ(siVar);
        this.ͺ = (Bitmap) aas.ˎ(bitmap);
        this.ʼ = this.ʼ.ॱ(new zp().ˏ((si) siVar));
        this.ॱˋ = aau.ˏ(bitmap);
        this.ᐝॱ = bitmap.getWidth();
        this.ॱᐝ = bitmap.getHeight();
    }

    Bitmap ˊ() {
        return this.ͺ;
    }

    void ˋ(b bVar) {
        if (this.ˋॱ) {
            throw new IllegalStateException("Cannot subscribe to a cleared frame loader");
        } else if (this.ˊ.contains(bVar)) {
            throw new IllegalStateException("Cannot subscribe twice in a row");
        } else {
            boolean isEmpty = this.ˊ.isEmpty();
            this.ˊ.add(bVar);
            if (isEmpty) {
                ᐝ();
            }
        }
    }

    void ˏ(b bVar) {
        this.ˊ.remove(bVar);
        if (this.ˊ.isEmpty()) {
            ˏॱ();
        }
    }

    int ॱ() {
        return this.ᐝॱ;
    }

    int ˏ() {
        return this.ॱᐝ;
    }

    int ˋ() {
        return this.ॱ.ʽ() + this.ॱˋ;
    }

    int ˎ() {
        return this.ᐝ != null ? this.ᐝ.ˏ : -1;
    }

    ByteBuffer ʻ() {
        return this.ॱ.ॱ().asReadOnlyBuffer();
    }

    int ʽ() {
        return this.ॱ.ˊ();
    }

    private void ᐝ() {
        if (!this.ʻ) {
            this.ʻ = true;
            this.ˋॱ = false;
            ˋॱ();
        }
    }

    private void ˏॱ() {
        this.ʻ = false;
    }

    void ॱॱ() {
        this.ˊ.clear();
        ˊॱ();
        ˏॱ();
        if (this.ᐝ != null) {
            this.ˎ.ˎ(this.ᐝ);
            this.ᐝ = null;
        }
        if (this.ˏॱ != null) {
            this.ˎ.ˎ(this.ˏॱ);
            this.ˏॱ = null;
        }
        if (this.ˊॱ != null) {
            this.ˎ.ˎ(this.ˊॱ);
            this.ˊॱ = null;
        }
        this.ॱ.ʻ();
        this.ˋॱ = true;
    }

    Bitmap ʼ() {
        return this.ᐝ != null ? this.ᐝ.ˎ() : this.ͺ;
    }

    private void ˋॱ() {
        if (this.ʻ && !this.ʽ) {
            if (this.ॱॱ) {
                aas.ˏ(this.ˊॱ == null, "Pending target must be null when starting from the first frame");
                this.ॱ.ᐝ();
                this.ॱॱ = false;
            }
            if (this.ˊॱ != null) {
                c cVar = this.ˊॱ;
                this.ˊॱ = null;
                ˏ(cVar);
                return;
            }
            this.ʽ = true;
            long ˎ = ((long) this.ॱ.ˎ()) + SystemClock.uptimeMillis();
            this.ॱ.ˋ();
            this.ˏॱ = new c(this.ˋ, this.ॱ.ˏ(), ˎ);
            this.ʼ.ॱ(zp.ˊ(ॱˊ())).ˋ(this.ॱ).ˏ(this.ˏॱ);
        }
    }

    private void ˊॱ() {
        if (this.ͺ != null) {
            this.ˏ.ˏ(this.ͺ);
            this.ͺ = null;
        }
    }

    @VisibleForTesting
    void ˏ(c cVar) {
        if (this.ॱˎ != null) {
            this.ॱˎ.ˊ();
        }
        this.ʽ = false;
        if (this.ˋॱ) {
            this.ˋ.obtainMessage(2, cVar).sendToTarget();
        } else if (this.ʻ) {
            if (cVar.ˎ() != null) {
                ˊॱ();
                c cVar2 = this.ᐝ;
                this.ᐝ = cVar;
                for (int size = this.ˊ.size() - 1; size >= 0; size--) {
                    ((b) this.ˊ.get(size)).ʻ();
                }
                if (cVar2 != null) {
                    this.ˋ.obtainMessage(2, cVar2).sendToTarget();
                }
            }
            ˋॱ();
        } else {
            this.ˊॱ = cVar;
        }
    }

    private static ro<Bitmap> ˎ(rp rpVar, int i, int i2) {
        return rpVar.ॱ().ॱ(((zp) ((zp) zp.ˎ(tf.ॱ).ˊ(true)).ॱ(true)).ˊ(i, i2));
    }

    private static sa ॱˊ() {
        return new aai(Double.valueOf(Math.random()));
    }
}
